#!/usr/bin/perl -w
use Getopt::Long;
use strict;
use FindBin;
use lib ("$FindBin::Bin/../perl_modules");
use die_error;
use run_cmd;



#-----------------------------------------------------------------------------------------
#	Get DB parameters
#
use db_parameters;
#
#	Get DB parameters
#-----------------------------------------------------------------------------------------




#88888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888
my $usage = <<"USAGE";

USAGE:

    kitsch_wrapper.pl
	              --cmd_submit QRSH_CMD (e.g."qrsh -now n -cwd -q  medium_jobs.q -p -5")
				  --path PATH_FILENAME_OF_SUBMATRIX
				  --file FILE_NAME_OF_SUBMATRIX
				  --error_path PATH_FILENAME_OF_ERROR
		
		This script runs kitsch locally if the files are small

USAGE

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# mandatory


# optional parameters
my $help = undef;
my $file      ;
my $path      ;
my $error_path;
my $cmd_submit;


GetOptions('help' => \$help,
		   'file=s' 		=> \$file,
		   'path=s' 		=> \$path,
		   'error_path=s' 	=> \$error_path,
		   'cmd_submit=s'	=> \$cmd_submit);

die $usage if ($help);
die "--file       missing\n" .$usage unless ($file      );
die "--path       missing\n" .$usage unless ($path      );
die "--cmd_submit missing\n" .$usage unless ($cmd_submit);
die "--error_path missing\n" .$usage unless ($error_path);


	my $cmd;
	#
	# run locally if small
	#
	use constant FILE_SIZE => 7;
	die unless -f "$path.sub_matrix";
	if ((stat("$path.sub_matrix"))[FILE_SIZE] < 4000)
	{
		print STDERR "\tLocal\n";
		$cmd = <<"        CMD";
		$dir_lg_share_bin/tree_kitsch \
					--input_format b \
					--iterations 30 \
					--weighting 3.0 \
					--header \
					--err_log $error_path \
					< $path.sub_matrix \
			> $path.kitsch
        CMD
	}
	else
	{
		print STDERR "\tRemote\n";
		$cmd = <<"        CMD";
			$cmd_submit -N "kitsch_$file" $dir_lg_share_bin/tree_kitsch \
					--input_format b \
					--iterations 30 \
					--weighting 3.0 \
					--header \
					--err_log $error_path \
					< $path.sub_matrix \
			> $path.kitsch
        CMD
	}
	#print STDERR $cmd;
	run_cmd($cmd);

